﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.DependencyInjection;
using Syspetro.Core.Extensions;
using Syspetro.Core.Resources;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Syspetro.Core.Localization
{
    public class Languages
    {
        public const string zhCN = "zh-CN";
        public const string zhHK = "zh-HK";
        public const string enUS = "en-US";
    }
    /// <summary>
    /// 切换语言：请求头设置accept-language或者请求参数设置culture
    /// </summary>
    public static class LanguageManager
    {
        static IList<CultureInfo> supportedCultures = new List<CultureInfo>
        {
            new CultureInfo(Languages.zhCN),
            new CultureInfo(Languages.zhHK),
            new CultureInfo(Languages.enUS),
        };
        public static void UseLocalizationLanguages(this IApplicationBuilder app)
        {
            app.UseRequestLocalization(new RequestLocalizationOptions
            {
                DefaultRequestCulture = new RequestCulture(Languages.zhCN),
                SupportedCultures = supportedCultures,
                ApplyCurrentCultureToResponseHeaders = true,
                SupportedUICultures = supportedCultures
            });
        }
        /// <summary>
        /// 支持本地化多语言
        /// </summary>
        /// <param name="services"></param>
        /// <param name="resourcesPath"></param>
        /// <param name="cultures"></param>
        public static void AddLocalizationLanguages(this IServiceCollection services, string resourcesPath = "Resources", params CultureInfo[] cultures)
        {
            if (cultures.Length > 0)
            {
                supportedCultures = cultures;
            }
          //  services.AddTransient<ISharedResource, SharedResource>();
            services.AddLocalization(options => options.ResourcesPath = resourcesPath);
            services.AddMvc().AddDataAnnotationsLocalization();
        }
    }
}
